package me.xingyannian.zhaopin.controller;

import me.xingyannian.zhaopin.dto.ChangCiDto;
import me.xingyannian.zhaopin.service.ChangCiService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import me.xingyannian.zhaopin.repository.ChangCiRepository;
import me.xingyannian.zhaopin.entity.ChangCi;

/**
 * @author xingyannian
 */
@Controller
@RequestMapping({"/changci", "/"})
public class ChangCiController {

    private final ChangCiService changCiService;

    public ChangCiController( ChangCiService changCiService) {
        this.changCiService = changCiService;
    }

    // 查看列表
    @RequestMapping({"/list", ""})
    public String list(Model model,@RequestParam(defaultValue = "1") int page) {
        // 设置默认值
        int pageNum = page - 1;
        // pageNum最小为0
        if (pageNum < 0) {
            pageNum = 0;
        }
        int pageSize = 10;
        //

        // 分页查询出所有没有被删除对象，并传递给视图
        Page<ChangCi> changcis = this.changCiService.findByIsDeletedFalseOrderByIdDesc(pageNum, pageSize);

        model.addAttribute("pageUrl", "/changci/list");
        model.addAttribute("changcis", changcis.getContent());
        // Page从0开始，前端从1开始，所以要加1
        model.addAttribute("currentPage", changcis.getNumber() + 1);
        model.addAttribute("totalPages", changcis.getTotalPages());

        return "changci/list";
    }
    /**
     * 添加功能接口
     * 该接口用于处理添加操作的请求，并返回对应的视图名称
     *
     * @return 返回视图名称 "changci/add"
     */
    // 添加
    @RequestMapping({"/add"})
    public String add() {
        return "changci/add";
    }
    // 保存
    @PostMapping({"/save"})
    public String save(@ModelAttribute ChangCiDto changCiDto) {
        if (changCiDto != null) {
            ChangCi changCi = null;
            if (changCiDto.getId() != null) {
                var changCiOptional = this.changCiService.findById(changCiDto.getId());
                if (changCiOptional.isPresent()) {
                    changCi = changCiOptional.get();
                }
            }else {
                changCi = new ChangCi();
            }
            if (changCi != null) {
                changCi.setName(changCiDto.getName());
                changCi.setDescription(changCiDto.getDescription());
            }
            this.changCiService.save(changCi);
        }else {
            //新增页面
            return "redirect:/changci/add";
        }
        return "redirect:/changci/list";
    }
    // 删除
    @GetMapping({"/delete"})
    public String delete(@RequestParam("id") Long id, @RequestParam("page") int page) {
        this.changCiService.delete(id);
        return "redirect:/changci/list?page=" + page;
    }
    // 编辑
    @GetMapping({"/edit"})
    public String edit(@RequestParam("id") Long id, Model model) {
        var changCi = this.changCiService.findById(id);
        changCi.ifPresent(ci -> model.addAttribute("changCi", ci));
        return "changci/edit";
    }
}
